DHT22 Sensor → SSD1306 OLED Display + NeoPixel Humidity Indicator
I²C 1-WIRE 3.3 V / 5 V WS2812B
This week I explored output devices by building a real-time environmental monitor driven by a DHT22 temperature and humidity sensor. The sensor feeds two independent output peripherals simultaneously:
Capacitive humidity + NTC temperature sensor. Single-wire proprietary protocol. Range: 0–100 % RH, −40–80 °C. Accuracy: ±2 % RH, ±0.5 °C. Minimum 2 s between readings.
128×64 pixel monochrome OLED. I²C interface at address 0x3C (some modules use 0x3D). Operates at 3.3 V or 5 V. Self-emissive — no backlight, excellent contrast in dim environments.
Addressable RGB LEDs daisy-chained on a single data line. 5 V, up to 60 mA per LED at full white. Controlled via Adafruit NeoPixel library. No extra clock wire needed.
ESP32 or Arduino Uno. ESP32 preferred — built-in I²C on GPIO 21/22, 3.3 V logic, and Wi-Fi for optional remote logging. Either board works at 3.3 V or 5 V.
10 kΩ pull-up resistor on DHT22 DATA line to VCC. 300–500 Ω series resistor on NeoPixel DIN to suppress signal ringing on long wire runs.
OLED ≈ 20 mA. NeoPixels at 80/255 brightness ≈ 80 mA total. DHT22 ≈ 1.5 mA. Total ≈ 102 mA — USB power from the development board is sufficient.
| DHT22 Pin | Connect To | Notes |
|---|---|---|
| 1 — VCC | 3.3 V or 5 V | Match your board's logic voltage |
| 2 — DATA | GPIO 4 (any digital pin) | Add 10 kΩ pull-up resistor from DATA to VCC |
| 3 — NC | — | Leave unconnected |
| 4 — GND | GND | Shared ground with MCU |
| OLED Pin | ESP32 | Arduino Uno |
|---|---|---|
| VCC | 3.3 V | 5 V |
| GND | GND | GND |
| SDA | GPIO 21 | A4 |
| SCL | GPIO 22 | A5 |
| NeoPixel Pin | Connect To | Notes |
|---|---|---|
| +5 V | 5 V rail | Must be 5 V — not 3.3 V |
| GND | GND (shared) | Common ground with MCU |
| DIN | GPIO 5 via 330 Ω | Series resistor placed close to the strip |
⚠ LEVEL SHIFTING: ESP32 outputs 3.3 V logic. NeoPixels nominally require 5 V on DIN. In practice 3.3 V usually works, but if LEDs glitch or show wrong colours, add a 74AHCT125 level shifter between GPIO and DIN.
💡 TIP: Place a 100 µF electrolytic capacitor across the 5 V and GND pads of the NeoPixel strip to smooth power spikes and protect the first LED from inrush current.
The firmware loop runs every 2 seconds — the minimum safe polling interval for the DHT22.
All four LEDs display the same colour simultaneously to form a clear visual band. Brightness is set to 80/255 (~31%) to be eye-safe and keep current draw within USB limits.
Written for Arduino IDE. Tested on ESP32 and Arduino Uno. See Section 06 for library installation.
Install all five via Sketch → Include Library → Manage Libraries in the Arduino IDE.
| Library | Author | Purpose |
|---|---|---|
| DHT sensor library | Adafruit | Read temperature & humidity from DHT22 |
| Adafruit Unified Sensor | Adafruit | Required dependency for the DHT library |
| Adafruit SSD1306 | Adafruit | Drive the SSD1306 OLED over I²C |
| Adafruit GFX Library | Adafruit | Graphics primitives — text, shapes, bitmaps |
| Adafruit NeoPixel | Adafruit | Control WS2812B addressable LEDs |
💡 TIP: When you install DHT sensor library, the IDE will prompt you to also install Adafruit Unified Sensor — click "Install All" to grab both at once.
Placed the DHT22 with its 10 kΩ pull-up, the OLED on the I²C bus, and ran the NeoPixel DIN through a 330 Ω resistor. Verified all shared grounds before powering up.
Uploaded a basic SSD1306 hello-world sketch to confirm the I²C address and verify the display worked before integrating the DHT22 and NeoPixels.
Added sensor readings and confirmed valid output in Serial Monitor. Tested the NaN guard by briefly disconnecting the data wire.
Tested the four humidity bands by breathing on the sensor to raise humidity (green → yellow → red) and using a fan to dry it out (blue). Adjusted colour thresholds accordingly.
[ Replace with your <video> tag or embed ]
Check the 10 kΩ pull-up on DATA. Ensure at least 2 s between readings — the DHT22 is slow by design. Try a different GPIO pin and confirm you declared the correct DHT_TYPE.
Run an I²C scanner sketch to find the actual address — some modules ship at 0x3D, not 0x3C. Confirm SDA and SCL are not swapped. Make sure display.display() is called — the framebuffer won't render without it.
Add a 300–500 Ω resistor in series with DIN, placed close to the strip. Add a 100 µF cap across 5 V / GND at the strip. If using ESP32 at 3.3 V, add a 74AHCT125 level shifter.
The strip is likely wired backwards — DIN and DOUT are directional. Check the arrow printed on the strip's PCB and ensure signal flows from MCU into the DIN end.
[ Write your personal reflection here. Some prompts to help: ]
← Back to Main Page